package com.example.dblibrary;

import android.app.Application;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.support.annotation.NonNull;

import com.example.dblibrary.database.AppDatabase;

import java.io.File;

public class AppApplication extends Application {

    private AppDatabase mAppDatabase;
    private final static String database_path = "/storage/99368f33-2b3e-4434-b440-2725ccee9a65/SOFMIT_DBBSM.db";

    @Override
    public void onCreate() {
        super.onCreate();
        mAppDatabase = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, new File("/storage/99368f33-2b3e-4434-b440-2725ccee9a65/SOFMIT_DBBSM.db").getAbsolutePath())
                .addMigrations(MIGRATION_1_2)
                .addCallback(new RoomDatabase.Callback() {
                    @Override
                    public void onCreate(@NonNull SupportSQLiteDatabase db) {
                        super.onCreate(db);
                    }

                    @Override
                    public void onOpen(@NonNull SupportSQLiteDatabase db) {
                        super.onOpen(db);
                    }
                }).build();
    }

    public AppDatabase getAppDatabase() {
        return mAppDatabase;
    }

    /**
     * 数据库版本 1->2 user表格新增了age列
     */
    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //			database.execSQL("ALTER TABLE User ADD COLUMN age integer");
        }
    };

    /**
     * 数据库版本 2->3 新增book表格
     */
    static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //			database.execSQL(
            //				"CREATE TABLE IF NOT EXISTS `book` (`uid` INTEGER PRIMARY KEY autoincrement, `name` TEXT , `userId` INTEGER, 'time' INTEGER)");
        }
    };

    /**
     * 数据库版本 2->3 新增book表格
     */
    static final Migration MIGRATION_3_4 = new Migration(3, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //			database.execSQL(
            //					"CREATE TABLE IF NOT EXISTS `book` (`uid` INTEGER PRIMARY KEY autoincrement, `name` TEXT , `userId` INTEGER, 'time' INTEGER)");
        }
    };

    /**
     * 数据库版本 2->3 新增book表格
     */
    static final Migration MIGRATION_4_5 = new Migration(4, 5) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //			database.execSQL(
            //					"CREATE TABLE IF NOT EXISTS `T_BDSM_SINGER` (`SINGER_ID` INTEGER PRIMARY KEY autoincrement, `SINGER_NAME` TEXT , `SINGER_TYPE` TEXT, 'SINGER_AREA' TEXT, 'SINGER_FIRST_SPELL' TEXT)");
        }
    };

    static final Migration MIGRATION_5_6 = new Migration(5, 6) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            //			database.execSQL(
            //					"CREATE TABLE IF NOT EXISTS `T_BDSM_SINGER` (`SINGER_ID` INTEGER PRIMARY KEY autoincrement, `SINGER_NAME` TEXT , `SINGER_TYPE` TEXT, 'SINGER_AREA' TEXT, 'SINGER_FIRST_SPELL' TEXT)");
        }
    };
}
